package handler

import (
	"day4/api/request"
	"day4/global"
	"day4/internal/dao"
	"day4/internal/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AddVideo(c *gin.Context) {
	var req request.AddVideo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	video := model.Videos{
		Name:    req.Name,
		Content: req.Content,
		Status:  req.Status,
	}
	dao.Create(&video)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": video,
	})
	return
}

func ListVideo(c *gin.Context) {
	var req request.ListVideo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var video []model.Videos
	dao.List(&video)
	global.DB.Order("desc name").Find(&video)
	c.JSON(http.StatusOK, gin.H{
		"code": 500,
		"msg":  "ok",
		"data": video,
	})
	return
}

func InfoVideo(c *gin.Context) {
	var req request.InfoVideo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var video model.Videos
	dao.GetOneLy(req.Id, &video)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": video,
	})
	return
}

func DelVideo(c *gin.Context) {
	var req request.DelVideo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var video model.Videos
	dao.Del(&video)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": video,
	})
	return
}
